<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/my_blog_list_common.css">
    <link rel="stylesheet" href="css/my_blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo1.jpg" alt="">
        <span class="title" style="font-size: 30px;font-style: italic;font-weight: 1000"><span style="color: #03a87f">B</span><span style="color: #ffffff">L</span><span style="color: #f3f6f5">O</span><span style="color: #57363e">G</span></span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html?type=all">主页</a>
        <a href="blog_list.html?id=1">博客大厅</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html" onclick="log_out()">退出账号</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3 id="username" style="font-size: 50px"></h3>
                <p  style="text-decoration: none;text-align: center;color: #6A6A6A" onclick="gitee()">gitee 地址</p>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="count"></span>
                    <span id="type_count"></span>
                </div>
            </div>
            <div class="card" id="sort">
                <div style="display: flex;justify-content: center;align-items: center">
                    <p>文章类型</p>
                </div>
                <div style="display: flex;flex-direction: column">
                    <div class="type">
                        <a class="all_type_content" href="myblog_list.html?type=all"style="text-align: center;font-weight: 600">全部</a>
                    </div>
                    <div id="type_content" class="type"style="display: flex;flex-wrap: wrap;flex-direction: column">

                    </div>
                </div>
            </div>
            <div class="card" id="recommend">
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="artDiv" class="container-right">
        </div>
        <div class="card" id="recommend_user">
        </div>
    </div>
    <script>
        var this_type=getUrlValue("type");
        function gitee(){
            location.href="https://gitee.com/HGtz2222/classroom_code/blob/master/java105/servlet/hello_servlet/src/main/java/login/IndexServlet.java";
        }
        function getUrlValue(key){
            var params=location.search;
            if(params.length>1){
                params=location.search.substring(1);
                var paramsarr=params.split("&");
                for(var i=0;i<paramsarr.length;i++){
                    var kv=paramsarr[i].split("=");
                    if(kv[0]==key){
                        return kv[1]
                    }
                }
                return "";
            }
        }
        function type(){
            var artListDiv ="";
            jQuery.ajax({
                url:"/article/type_content",
                type:"post",
                data:{},
                success:function (result){
                    for (let i = 0; i < result.data.length; i++) {
                        artListDiv += '<div class="type">';
                        artListDiv += '<a style="text-align: center" class="type_content" href="myblog_list.html?type='+result.data[i]+'">'+result.data[i]+'</a>';
                        artListDiv += '</div>';
                    }
                    jQuery("#type_content").html(artListDiv);

                }
            })
        }
        type();
        function hot_article_count(){
            var artListDiv ="<span style='color: red;text-align: center;font-weight: 1000;margin-left: 154px;margin-right: 72px'>热门文章</span>"+'<hr>';
            jQuery.ajax({
                url:"/article/hot_count",
                type:"get",
                data:{},
                success:function (result){
                    console.log(result.data);
                    for (var i=0;i<result.data.length;i++){
                        artListDiv += '<div style="display: flex">'
                        artListDiv += '<div style="flex: 2">'+(i+1)+'.  '+result.data[i].title+'</div>'
                        artListDiv += '<div style="flex: 1">'+'热值'+result.data[i].hot_count+'</div>'
                        artListDiv += '<div style="flex: 1">'+'@'+result.data[i].username+'</div>'
                        artListDiv += '</div>'
                    }
                    jQuery("#recommend_user").html(artListDiv);
                }
            })
        }
        hot_article_count();
        function log_out(){
            jQuery.ajax({
                url:"/user/log_out",
                type: "POST",
                data:{},
                success:function (result){
                    if (result.data==1){
                        alert("退出登陆成功");
                        location.href="/reg.html";
                    }
                }
            })
        }
        function getuser(){
            var username=jQuery("#username");
            var count=jQuery("#count");
            var type_count=jQuery("#type_count");
            jQuery.ajax({
                url:"/user/infor_user",
                type:"POST",
                data:{},
                success:function (result){
                    if (result!=null&&result.data.count>=0){
                        username.text(result.data.username);
                        count.text(result.data.count);
                        type_count.text(result.data.type_count);
                    }else{
                        alert("用户信息查询错误");
                    }
                }
            })
        }
        function getarticle(){
            jQuery.ajax({
                url:"/article/getarticle",
                type:"POST",
                data:{"type":this_type},
                success:function(result){
                    if(result!=null){
                        // 有两种情况，一种是发表了文章，一种是没有发表任何文章
                        if(result.data!=null && result.data.length>0){
                            // 此用户发表文章了
                            var artListDiv ="";
                            for(var i=0;i<result.data.length;i++){
                                var artItem = result.data[i];
                                artListDiv += '<div class="blog" id='+artItem.id+'>';
                                artListDiv += '<div class="title">'+artItem.title+'</div>';
                                artListDiv += '<div class="date">'+artItem.updatetime+'</div>';
                                artListDiv += '<div class="desc">';
                                artListDiv += artItem.content.slice(0,75);
                                artListDiv += '<br>';
                                artListDiv += artItem.content.slice(76,106);
                                artListDiv += '.........';
                                artListDiv += '</div>';
                                artListDiv += '<div style="display: flex;justify-content: center">'
                                artListDiv += '<a href="blog_content.html?id='+
                                    artItem.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
                                artListDiv += '<a href="blog_edit.html?id='+
                                    artItem.id + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
                                artListDiv += '<a href="javascript:myDel('+
                                    artItem.id+');" class="detail" onclick="articledelete('+artItem.id+')">删除 &gt;&gt;</a>';
                                artListDiv += '</div>'
                                artListDiv += '</div>';
                            }
                            jQuery("#artDiv").html(artListDiv);
                        }else{
                            // 当前用户从未发过任何文章
                            jQuery("#artDiv").html("<h3 style='text-align: center'>暂无文章~</h3>");
                        }
                    }else{
                        alert("查询文章列表出错，请重试！");
                    }
                }
            });
        }
        function nice_user(){
            var artListDiv ="<span style='color: red;text-align: center;font-weight: 1000;margin-left: 72px;margin-right: 72px'>热榜作者</span>"+'<hr>';
            jQuery.ajax({
                url:"/article/nice_user",
                type:"get",
                data:{},
                success:function (result){
                    for (i=0;i < result.data.length;i++){
                        // var body=result.data[i]
                        artListDiv += '<div style="display: flex;">';
                        artListDiv += '<div style="flex: 2">'+(i+1)+'.  @'+result.data[i].username+'</div>'
                        artListDiv += '<div style="flex: 1">'+'热度: '+result.data[i].activ_count+'</div>'
                        artListDiv += '</div>'
                    }
                    jQuery("#recommend").html(artListDiv);
                }
            })
        }
        nice_user();
        function articledelete(id){
            jQuery.ajax({
                url:"/article/delete",
                type:"POST",
                data:{"id":id},
                success:function (result){
                    if (result!=null&&result.data>0){
                        alert("删除成功");
                        location.href=location.href;
                    }else {
                        alert("删除失败");
                    }
                }
            })
        }
        getuser();
        getarticle();
    </script>
</body>

</html>